package com.lanou.zakerproject.disport;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.VolleySingleton;
import com.lanou.zakerproject.tool.GsonRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class DisportFragment extends Fragment {
    private ExpandableListView expandableListView;
    private DisportExAdapter disportExAdapter;
    private Context context;
    private DisportData disportData;
    String url = "http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.4.5&_version=6.44&c=columns";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_disport, null);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView_news);
        expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                    Intent intent = new Intent();
                    intent.setClass(context, DisportGroupActivity.class);
                    context.startActivity(intent);
                    Toast.makeText(context, "PACKED_POSITION_TYPE_CHILD", Toast.LENGTH_SHORT).show();
                    return true;
                } else if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                    Intent intent = new Intent();
                    String pk = disportData.getData().getColumns().get(position).getPk();
                    String title = disportData.getData().getColumns().get(position).getTitle();
                    intent.putExtra("title", title);
                    intent.putExtra("pk", pk);
                    intent.setClass(context, DisportGroupActivity.class);
                    context.startActivity(intent);
                    return true;
                }
                return false;
            }
        });

//        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
//            @Override
//            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//                Intent intent = new Intent();
//                intent.setClass(context, DisportGroupActivity.class);
//                context.startActivity(intent);
//                Toast.makeText(context, "SetOnGroupClickListener", Toast.LENGTH_SHORT).show();
//                return true;
//            }
//        });
//        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
//            @Override
//            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//                Intent intent = new Intent();
//                intent.setClass(context, DisportGroupActivity.class);
//                context.startActivity(intent);
//                Toast.makeText(context, "setOnChildClickListener", Toast.LENGTH_SHORT).show();
//                return true;
//            }
//        });
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getRequsetData(url);


    }

    /**
     * 解析数据的方法
     *
     * @param urls 接口
     */
    private void getRequsetData(String urls) {
        GsonRequest<DisportData> gsonRequest = new GsonRequest<>(urls, DisportData.class, new Response.Listener<DisportData>() {
            @Override
            public void onResponse(DisportData response) {
                disportExAdapter = new DisportExAdapter(getActivity(), response);
                expandableListView.setAdapter(disportExAdapter);
                disportData = response;
                //将所有项设置成默认展开
                int groupCount = expandableListView.getCount();
                for (int i = 0; i < groupCount; i++) {
                    expandableListView.expandGroup(i);
                }
                //设置 属性 GroupIndicator 去掉默认向下的箭头
                expandableListView.setGroupIndicator(null);
                //不能关闭
                expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                        return true;
                    }
                });

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(gsonRequest);
    }
}
